package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.basic.common.utils.*;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import com.basic.modules.ykf.entity.YkfDictDetailEntity;
import com.basic.modules.ykf.entity.YkfDictEntity;
import com.basic.modules.ykf.entity.YkfShopDistributeRuleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.basic.modules.ykf.dao.YkfCompanyDeptDao;
import com.basic.modules.ykf.entity.YkfCompanyDeptEntity;
import com.basic.modules.ykf.service.YkfCompanyDeptService;

import java.util.List;


@Service("ykfCompanyDeptService")
public class YkfCompanyDeptServiceImpl extends ServiceImpl<YkfCompanyDeptDao, YkfCompanyDeptEntity> implements YkfCompanyDeptService {

    @Autowired
    private SysUserService userService;
    @Autowired
    private YkfDictServiceImpl ykfDictService;
    @Autowired
    private YkfDictDetailServiceImpl ykfDictDetailService;

    @Override
    public PageUtils queryPage(ParamsModel<YkfCompanyDeptEntity> params) {
        if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
            ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
            sortParams.setSidx("createTime");
            sortParams.setOrder("desc");
            params.setSort(sortParams);
        }
        QueryWrapper<YkfCompanyDeptEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        List<String> powerCompanyNos = userService.getPowerCompanyNos();
        if (ObjectUtil.isNotEmpty(powerCompanyNos)) {
            if (!powerCompanyNos.contains("ALL")) {
                queryWrapper.lambda().in(YkfCompanyDeptEntity::getCompanyNo, powerCompanyNos);
            }
        } else {
            return new PageUtils();
        }
        IPage<YkfCompanyDeptEntity> page = this.page(
                new Query<YkfCompanyDeptEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        if (ObjectUtil.isNotEmpty(page) && ObjectUtil.isNotEmpty(page.getRecords())) {
            List<YkfCompanyDeptEntity> list = page.getRecords();
            QueryWrapper<YkfDictEntity> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.lambda().eq(YkfDictEntity::getDictNo, "company")
                    .last("limit 1");
            YkfDictEntity one = ykfDictService.getOne(queryWrapper2);
            if (ObjectUtil.isNotEmpty(one)) {
                for (YkfCompanyDeptEntity item : list) {
                    //查询最新的公司名称
                    QueryWrapper<YkfDictDetailEntity> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.lambda().eq(YkfDictDetailEntity::getVal, item.getCompanyNo())
                            .eq(YkfDictDetailEntity::getDictId, one.getDictId())
                            .last("limit 1");
                    YkfDictDetailEntity dictDetail = ykfDictDetailService.getOne(queryWrapper1);
                    if (ObjectUtil.isNotEmpty(dictDetail)) {
                        item.setCompanyName(dictDetail.getName());
                    }
                }
            }

        }
        return new PageUtils(page);
    }


    @Override
    public PageUtils currentCompanyDeptPage(ParamsModel<SysUserEntity> params) {
        if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
            ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
            sortParams.setSidx("createTime");
            sortParams.setOrder("desc");
            params.setSort(sortParams);
        }
        SysUserEntity user = CommonUtils.getUser();
        if (ObjectUtil.isNotEmpty(user) && StrUtil.isNotBlank(user.getCompanyNo())) {
            String companyNo = user.getCompanyNo();
            //查询市场部部门ID
            QueryWrapper<YkfCompanyDeptEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(YkfCompanyDeptEntity::getCompanyNo, companyNo)
                    .eq(YkfCompanyDeptEntity::getDeptName, "市场部")
                    .last("limit 1");
            YkfCompanyDeptEntity one = this.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(one)) {
                QueryWrapper<SysUserEntity> queryWrapper1 = ParamUtils.createQueryWrapperByDefaultSort(params);
                queryWrapper1.lambda().eq(SysUserEntity::getDeptNo, one.getDeptId());
                if (ObjectUtil.isNotEmpty(params.getCondition().getKeyword())) {
                    String keyword = params.getCondition().getKeyword();
                    queryWrapper1.lambda().and(wrapper -> wrapper.like(SysUserEntity::getName, keyword)
                            .or().like(SysUserEntity::getFlowerName, keyword)
                            .or().like(SysUserEntity::getUserId, keyword)
                            .or().like(SysUserEntity::getNo, keyword)
                            .or().like(SysUserEntity::getMobile, keyword));
                }
                IPage<SysUserEntity> page = userService.page(
                        new Query<SysUserEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                        queryWrapper1
                );
                return new PageUtils(page);
            }
        }
        return null;
    }
}
